package com.api.demo.controller;

import com.api.demo.entity.Content;
import com.api.demo.entity.User;
import com.api.demo.mapper.ContentLikeMapper;
import com.api.demo.mapper.ContentMapper;
import com.api.demo.service.ContentService;
import com.api.demo.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.Data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/content")
public class ContentController {

    @Autowired
    private ContentService contentService;

    @Autowired
    private UserService userService;

    @Autowired
    private ContentLikeMapper contentLikeMapper;

    @PostMapping
    public ResponseEntity<?> createContent(@RequestBody Content content, Authentication authentication) {
        content.setUserId(getUserIdFromUsername(authentication.getName()));
        Content savedContent = contentService.createContent(content);
        return ResponseEntity.ok(savedContent);
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> updateContent(
            @PathVariable Long id,
            @RequestBody Content content,
            Authentication authentication) {
        content.setId(id);
        content.setUserId(getUserIdFromUsername(authentication.getName()));
        Content updatedContent = contentService.updateContent(content);
        return ResponseEntity.ok(updatedContent);
    }

    @GetMapping
    public ResponseEntity<?> getContentList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        IPage<Content> contentPage = contentService.getContentList(new Page<>(page, size));
        return ResponseEntity.ok(contentPage);
    }

    @GetMapping("/seeHome")
    public ResponseEntity<?> seeHome(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam Long homeUserId) {
        IPage<Content> contentPage = contentService.homeContent(
                new Page<>(page, size),
                homeUserId);
        return ResponseEntity.ok(contentPage);
    }

    @GetMapping("/my")
    public ResponseEntity<?> getMyContentList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status,
            Authentication authentication) {
        IPage<Content> contentPage = contentService.getMyContentList(
                new Page<>(page, size),
                getUserIdFromUsername(authentication.getName()),
                status);
        return ResponseEntity.ok(contentPage);
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getContentDetail(@PathVariable Long id, Authentication authentication) {
        Long userId = getUserIdFromUsername(authentication.getName());
        Content content = contentService.getContentDetail(id);
        ContentDetails contentDetails = new ContentDetails();
        contentDetails.setContent(content);
        contentDetails.setLike(contentLikeMapper.findByUserIdAndContentId(userId, content.getId()) != null);
        return ResponseEntity.ok(contentDetails);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteContent(
            @PathVariable Long id,
            Authentication authentication) {
        contentService.deleteContent(id, getUserIdFromUsername(authentication.getName()));
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/like")
    public ResponseEntity<?> likeContent(
            @PathVariable Long id,
            Authentication authentication) {
        contentService.likeContent(id, getUserIdFromUsername(authentication.getName()));
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}/like")
    public ResponseEntity<?> unlikeContent(
            @PathVariable Long id,
            Authentication authentication) {
        contentService.unlikeContent(id, getUserIdFromUsername(authentication.getName()));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/search")
    public ResponseEntity<?> searchContent(
            @RequestParam String keyword,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        IPage<Content> contentPage = contentService.searchContent(keyword, type, status, new Page<>(page, size));
        return ResponseEntity.ok(contentPage);
    }

    private Long getUserIdFromUsername(String username) {
        User user = userService.findByUsername(username);
        if (user == null) {
            throw new IllegalArgumentException("用户不存在");
        }
        return user.getId();
    }

    @Data
    public static class ContentDetails {
        private Content content;
        private boolean isLike;
    }
}